package com.test.deencredit.analytics_deencredit.util_deencredit

import android.app.Application
import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.test.deencredit.analytics_deencredit.bean_deencredit.PamGoogleInstall_deencredit
import com.test.deencredit.analytics_deencredit.http_deencredit.HttpHelper_deencredit
import com.test.deencredit.analytics_deencredit.timezone_deencredit.TimeZoneUtil_deencredit
import com.test.deencredit.base_deencredit.BaseApplication_deencredit
import com.test.deencredit.config_deencredit.CountryConfig_deencredit
import com.test.deencredit.config_deencredit.Paths_deencredit
import java.sql.DriverManager.println

object GoogleInstallUtil_deencredit {

    /**
     * google play 返回的install referrer，
     * 建议每个接口都需要带上这个字段
     */
    private var mInstallReferrer_deencredit: String = ""

    /** 句柄 */
    private var mInstallReferrerClient_deencredit: InstallReferrerClient =
        InstallReferrerClient.newBuilder(BaseApplication_deencredit.getInstant()).build()

    /**
     * 初始化google install referref模块
     *
     * @param context Application
     */
    fun init_deencredit(context: Application) {
        init_deencredit(context) { saveS2sInfo_deencredit(mutableListOf(CountryConfig_deencredit.getInstance_deencredit().commonPath_deencredit)) }
    }

    /**
     * 初始化google install referref模块
     *
     * @param context Application
     * @param success 成功回调
     */
    fun init_deencredit(context: Application, success: (String) -> Unit) {
        try {
            if (mInstallReferrer_deencredit.isBlank()) {
                mInstallReferrerClient_deencredit.startConnection(object :
                    InstallReferrerStateListener {
                    override fun onInstallReferrerSetupFinished(responseCode: Int) {
                        when (responseCode) {
                            InstallReferrerClient.InstallReferrerResponse.OK -> {
                                try {
                                    println("google install call back")
                                    val response = mInstallReferrerClient_deencredit.installReferrer
                                    mInstallReferrer_deencredit = response?.installReferrer ?: ""
                                    mInstallReferrerClient_deencredit.endConnection()

                                    //回调通知
                                    success.invoke(mInstallReferrer_deencredit)

                                } catch (e: Exception) {
                                    e.printStackTrace()
                                    mInstallReferrerClient_deencredit.endConnection()
                                }
                            }
                            InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED -> {

                                println("FEATURE_NOT_SUPPORTED")
                                mInstallReferrerClient_deencredit.endConnection()
                            }
                            InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE -> {

                                println("SERVICE_UNAVAILABLE")
                                mInstallReferrerClient_deencredit.endConnection()
                            }
                        }
                    }

                    override fun onInstallReferrerServiceDisconnected() {
                    }
                })
            } else {
                //回调通知
                success.invoke(mInstallReferrer_deencredit)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /**
     * 获取Install Referrer
     */
    fun getInstallReferrer_deencredit(): String {
        return mInstallReferrer_deencredit
    }

    /**
     * 保存google s2s信息
     */
    fun saveS2sInfo_deencredit(targetUrlList: List<String>) {
        val params =
            PamGoogleInstall_deencredit(
                channelCode = getInstallReferrer_deencredit(),
                subChannelCode = getInstallReferrer_deencredit()
            )
        val body = params.toEncryptParams(Paths_deencredit.PATH_GP_INSTALL_TEMP_deencredit)
//        val body = Gson().toJson(params).trim()
        val resultTargetUrlList = TimeZoneUtil_deencredit.covertToRealTargetServers_deencredit(targetUrlList)
        kotlin.io.println("====== $resultTargetUrlList")

        //传多国时，里触发多次
        resultTargetUrlList.forEach { path ->
//            val pathFormat =
//                if (path.endsWith(Url.PATH_GP_INSTALL_TEMP)) path else (path + Url.PATH_GP_INSTALL_TEMP)
            HttpHelper_deencredit.postData_deencredit(path, body)
        }
    }
}